<template>
  <div class="login-container">
    <div class="login-header">
      <h3 class="title">触手联盟后台管理</h3>
      <h4 class="description">please login your account</h4>
    </div>
    <el-tabs class ="login-tap" v-model="activeName">
      <el-tab-pane class="tap-panel" label="验证码登录" name="first">
        <el-form autoComplete="on" :model="phoneForm" :rules="phoneLoginRules" ref="phoneForm" label-position="left">
          <el-form-item prop="phone">
            <span class="svg-container svg-container_login">
              <svg-icon icon-class="phone" />
            </span>
            <el-input name="phone" type="text" v-model="phoneForm.phone"   autoComplete="on" placeholder="请输入手机号码" />
          </el-form-item>
          <el-form-item prop="code">
            <el-input name="code" class="el-input-w"  v-model="phoneForm.code" type="text" @keyup.enter.native="handleLogin" autoComplete="on"
                      placeholder="验证码"></el-input>
            <span v-show="show" class="code" @click="getCode">获取验证码</span>
            <span v-show="!show" v-if="count>=10" class="count">{{count}} s</span>
            <span v-show="!show" v-else="count<10" class="count">0{{count}} s</span>
          </el-form-item>
          <el-form-item class="el-form-item-border">
            <el-button style="width:100%;" :loading="loading" @click.native.prevent="handleLogin">
              登录
            </el-button>
          </el-form-item>
          <div class="tips">
            <span class="blue" @click="handleForget">忘记密码</span>
            <span>没有账号?</span>
            <span class="blue" @click="handleRegister">立即注册</span>
          </div>
        </el-form>
      </el-tab-pane>
      <el-tab-pane class="" label="密码登录" name="second">
        <el-form autoComplete="on" :model="userForm" :rules="userLoginRules" ref="userForm" label-position="left">
          <el-form-item prop="phone">
            <span class="svg-container svg-container_login">
              <svg-icon icon-class="user" />
            </span>
            <el-input name="phone" type="text" v-model="userForm.phone" autoComplete="on" placeholder="请输入手机号码" />
          </el-form-item>
          <el-form-item prop="password">
            <span class="svg-container svg-container_login">
              <svg-icon icon-class="password" />
            </span>
            <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="userForm.password" autoComplete="on"
                      placeholder="请输入密码"></el-input>
            <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye" /></span>
          </el-form-item>
          <el-form-item class="el-form-item-border">
            <el-button style="width:100%;" :loading="loading" @click.native.prevent="handleLogin">
              登录
            </el-button>
          </el-form-item>
          <div class="tips">
            <span class="blue" @click="handleForget">忘记密码</span>
            <span>没有账号?</span>
            <span class="blue" @click="handleRegister">立即注册</span>
          </div>
        </el-form>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { validateMobile, validateCode } from '@/utils/validate'

export default {
  name: 'login',
  data() {
    const validatePhone = (rule, value, callback) => {
      if (!validateMobile(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    const validateVerifyCode = (rule, value, callback) => {
      if (!validateCode(value)) {
        callback(new Error('请输入6位有效的验证码'))
      } else {
        callback()
      }
    }
    return {
      activeName: 'first',
      phoneForm: {
        phone: '',
        code: '',
        type: ''
      },
      userForm: {
        phone: '',
        password: '',
        type: ''
      },
      phoneLoginRules: {
        phone: [{ required: true, message: '请输入正确的手机号', trigger: 'blur', validator: validatePhone }],
        code: [{ required: true, trigger: 'blur', validator: validateVerifyCode }]
      },
      userLoginRules: {
        phone: [{ required: true, message: '请输入正确的手机号', trigger: 'blur', validator: validatePhone }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      loading: false,
      showDialog: false,
      show: true,
      count: '',
      timer: null,
      second: 60,
      pwdType: 'password'
    }
  },
  methods: {
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    getCode() {
      this.$refs.phoneForm.validateField('phone', (valid) => {
        if (!valid.length) {
          const data = {}
          data.phone = this.phoneForm.phone
          this.$store.dispatch('Smscode', data).then(() => {
            if (!this.timer) {
              this.count = this.second
              this.show = false
              this.timer = setInterval(() => {
                if (this.count > 0 && this.count <= this.second) {
                  this.count--
                } else {
                  this.show = true
                  clearInterval(this.timer)
                  this.timer = null
                }
              }, 1000)
            }
          })
        }
      })
    },
    handleForget() {
      this.$router.push({ path: '/forget' })
    },
    handleRegister() {
      this.$router.push({ path: '/register' })
    },
    handleLogin() {
      if (this.activeName === 'first') {
        this.$refs.phoneForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.phoneForm.type = 1
            this.$store.dispatch('Login', this.phoneForm).then(() => {
              this.loading = false
              this.$store.dispatch('GenerateRoutes').then(() => {
              })
              this.$router.push({ path: '/' })
            }).catch(() => {
              this.loading = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      } else if (this.activeName === 'second') {
        this.$refs.userForm.validate(valid => {
          if (valid) {
            this.loading = true
            this.userForm.type = 2
            this.$store.dispatch('Login', this.userForm).then(() => {
              this.loading = false
              this.$router.push({ path: '/' })
            }).catch(() => {
              this.loading = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg:#2d3a4b;
$light_gray:#eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 3px solid rgba(255, 255, 255, 0.1);
    background: $bg;
    border-radius: 5px;
    color: #454545;
  }
  .el-form-item-border{
    border: none;
  }
  .el-input-w {
    width: 68%;
  }
  .el-tabs__active-bar {
    position: absolute;
    bottom: 0;
    left: 0;
    height: 2px;
    background-color: #16b8bd;
    z-index: 1;
    -webkit-transition: -webkit-transform .3s cubic-bezier(.645,.045,.355,1);
    transition: -webkit-transform .3s cubic-bezier(.645,.045,.355,1);
    transition: transform .3s cubic-bezier(.645,.045,.355,1);
    transition: transform .3s cubic-bezier(.645,.045,.355,1), -webkit-transform .3s cubic-bezier(.645,.045,.355,1);
    transition: transform .3s cubic-bezier(.645,.045,.355,1),-webkit-transform .3s cubic-bezier(.645,.045,.355,1);
    list-style: none;
  }
  .el-tabs__item.is-active {
    color: #16b8bd;
  }
  .el-tabs__item:hover {
    color: #16b8bd;
    cursor: pointer;
  }
  .el-tabs__item {
    padding: 0 20px;
    height: 40px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    line-height: 40px;
    display: inline-block;
    list-style: none;
    font-size: 14px;
    font-weight: 500;
    color: #ffffff;
    position: relative;
  }


}

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background: url("../../assets/images/bg.jpg");
  .login-header{
    width: 520px;
    padding: 35px 35px 0px 35px;
    margin: 220px auto;
  }
  .login-tap{
    position: absolute;
    top:300px;
    left: 0;
    right: 0;
    width: 520px;
    padding: 35px 35px 15px 35px;
    margin: 20px auto;
    min-height: 400px;
    color:#fff
  }
  .tap-panel{
    color:#fff
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    width:100%;
    float: right;
    span {
      &:first-of-type {
        margin-right: 265px;
        color: #16b0b8;
        text-decoration: underline;
        cursor: pointer;
      }
      &:last-of-type {
        color: #16b0b8;
        text-decoration: underline;
        cursor: pointer;
      }
    }
  }
  .svg-container {
    padding-left: 11px;
    color: #fff;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title {
    letter-spacing:5px;
    word-spacing:5px;
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 20px auto;
    text-align: center;
    font-weight: bold;
  }
  .description{
    color: #eee;
    margin: 0px auto 40px auto;
    text-align: center;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .code{
    text-align: center;
    background-color: rgba(255, 255, 255, 0.1);
    width: 138px;
    height: 47px;
    line-height: 49px;
    color: #ffff;
    display: inline-block;
    cursor: pointer;
    border: none;
  }
  .count{
    text-align: center;
    background-color: rgba(255, 255, 255, 0.1);
    width: 138px;
    height: 47px;
    line-height: 49px;
    color: #ffff;
    display: inline-block;
    cursor: pointer;
    border: none;
  }
}
</style>
